Program add_ann_date_episodes
      character*6 ticker(5500)
      character*8 cusp(5500),cusip(450000),lcusip,fcusip(50000)
      integer(kind=8):: nshrs,nvalue,jvalue,ivol(5500),numshrs(5500),ntot(5500),navail(5500)
      dimension nep(5500),ntype(5500),jyr(5500),jmn(5500),jdy(5500),retrn(5500),pctonloan(5500),ishrout(5500),vwret(5500),util(5500) 
      dimension jast(5500),itickt(5500),age(5500),favg(5500),fmax(5500),fmin(5500),fstd(5500),iperm(5500),price(5500),ncbeg(50000),iadum(5500)
      dimension jdate(5500),ndate(450000),nhr(450000),jhr(5500)
!      open(unit=10,file='e:\FIS_duration\qtr_earn_annc_time.prn')
!      open(unit=11,file='e:\FIS_duration\episode_data_type_total.txt')
!      open(unit=98,file='e:\FIS_duration\missing_cusips_for_earn_ann.txt')
!      open(unit=99,file='e:\FIS_duration\episode_data_total_anndate.txt')
      open(unit=10,file='e:\FIS_duration\qtr_earn_annc_time.prn')
      open(unit=11,file='e:\FIS_duration\episode_data_type_total_rep.txt')
      open(unit=98,file='e:\FIS_duration\missing_cusips_for_earn_ann_rep.txt')
      open(unit=99,file='e:\FIS_duration\episode_data_total_anndate_rep.txt')
      lcusip='xxxxxxxx'
      nc=0
      n=0
1     n=n+1
2     read(10,1000,end=9)cusip(n),ndate(n),nhr(n)
!      write(6,1000)cusip(n),ndate(n),nhr(n)
1000  format(1x,a8,1x,i8,1x,i2)    
      if(cusip(n).eq.'        ') go to 2
      if(cusip(n).eq.lcusip) go to 1
      lcusip=cusip(n)
      nc=nc+1
      fcusip(nc)=cusip(n)
      ncbeg(nc)=n
      go to 1
9     continue   
      ncnt=n-1
      iend=0
      i=0
10    i=i+1      
      read(11,1100,end=900)nep(i),ntype(i),jast(i),jyr(i),jmn(i),jdy(i),cusp(i),ticker(i),itickt(i),numshrs(i),age(i),favg(i),fmax(i),fmin(i),fstd(i),iperm(i),price(i),ivol(i),retrn(i),ishrout(i),vwret(i),util(i),navail(i),ntot(i)
1100  format(1x,i4,1x,i1,1x,i8,1x,i4,2i2,1x,a8,2x,a6,4x,i9,1x,i14,5(1x,f11.4),1x,i5,1x,f9.2,1x,i10,1x,f10.6,1x,i9,1x,f9.6,1x,f6.2,1x,i13,1x,i12)
      jdate(i)=jyr(i)*10000+jmn(i)*100+jdy(i)
      if(nep(i).eq.nep(1)) go to 10
15    icnt=i-1
      iadum=0
      jhr=0
      nn=0
20    nn=nn+1
!      write(6,6001)nn,nc
6001  format(2(1x,i6))      
      if(nn.gt.nc) write(98,9800)cusp(1),iperm(1),jdate(1),jdate(icnt)
      if(nn.gt.nc) write(6,9800)cusp(1),iperm(1),jdate(1),jdate(icnt)
9800  format(1x,a8,1x,i5,1x,i8,1x,i8)      
      if(nn.gt.nc) go to 90
      if(cusp(1).ne.fcusip(nn)) go to 20
      write(6,6000)cusp(1),fcusip(nn)
6000  format(2(1x,a8))      
      do 30 ii=1,icnt
          n2=ncbeg(nn)-1
22        n2=n2+1
          if(n2.gt.ncnt) go to 30
      if(cusip(n2).ne.cusp(ii)) go to 30
      if(cusip(n2).eq.cusp(ii).and.ndate(n2).gt.jdate(ii)) go to 30
      if(ndate(n2).ne.jdate(ii)) go to 22
      iadum(ii)=1
      jhr(ii)=nhr(n2)
30    continue
90    do 95 k=1,icnt      
95    write(99,9900)nep(k),ntype(k),jast(k),jdate(k),cusp(k),ticker(k),itickt(k),numshrs(k),age(k),favg(k),fmax(k),fmin(k),fstd(k),iperm(k),price(k),ivol(k),retrn(k),ishrout(k),vwret(k),util(k),navail(k),ntot(k),iadum(k),jhr(k)
9900  format(1x,i4,1x,i1,1x,i8,1x,i8,1x,a8,2x,a6,4x,i9,1x,i14,5(1x,f11.4),1x,i5,1x,f9.2,1x,i10,1x,f10.6,1x,i9,1x,f9.6,1x,f6.2,1x,i13,1x,i12,1x,i1,1x,i2)
      
100   continue
      nep(1)=nep(i)
      ntype(1)=ntype(i)
      jyr(1)=jyr(i)
      jmn(1)=jmn(i)
      jdy(1)=jdy(i)
      jdate(1)=jdate(i)
      retrn(1)=retrn(i)
      vwret(1)=vwret(i)
      ntot(1)=ntot(i)
      numshrs(1)=numshrs(i)
      navail(1)=navail(i)
      jast(1)=jast(i)
      cusp(1)=cusp(i)
      ticker(1)=ticker(i)
      itickt(1)=itickt(i)
      age(1)=age(i)
      favg(1)=favg(i)
      fmax(1)=fmax(i)
      fmin(1)=fmin(i)
      fstd(1)=fstd(i)
      iperm(1)=iperm(i)
      price(1)=price(i)
      ivol(1)=ivol(i)
      ishrout(1)=ishrout(i)
      util(1)=util(i)
      ntot(1)=ntot(i)
      i=1
      go to 10
900   continue
      iend=iend+1
      if(iend.eq.1) go to 15
      stop
end program add_ann_date_episodes